package com.etoak.controller;


import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Ingredients;
import com.etoak.service.IngredientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author etoak
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/ingredients")
public class IngredientsController {

    @Autowired
    IngredientsService ingredientsService;

    // 添加 post/ingredients
    @PostMapping
    public ResultVO<Object> add(@Validated @RequestBody Ingredients ingredients){
         ingredientsService.add(ingredients);
         return ResultVO.success();
    }

    // 条件查询 分页
    @GetMapping("/list")
    public ResultVO<Object> list(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            Ingredients ingredients){
        return ResultVO.success(ingredientsService.getList(pageNum,pageSize,ingredients));
    }

    // 更新接口
    @PutMapping("/{id}")
    public ResultVO<Object> update(@PathVariable int id,@RequestBody Ingredients ingredients){
        ingredientsService.update(id,ingredients);
        return ResultVO.success();
    }

    // 删除接口
    @DeleteMapping("/{id}")
    public ResultVO<Object> delete(@PathVariable int id){
        ingredientsService.delete(id);
        return ResultVO.success();
    }


    // 根据类型查询食材列表
    @GetMapping("/listByType")
    public ResultVO<List<Ingredients>> listByType(@RequestParam String type){
        List<Ingredients> list = ingredientsService.listByType(type);
        return ResultVO.success(list);
    }




}

